<template>
  <div>
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body :before-close="close">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" class="demo-ruleForm">

        <el-form-item label="公众号名称" prop="official_name">
          <el-input
            v-model="form.official_name"
            placeholder="请输入公众号名称"
          />
        </el-form-item>

        <!--        <el-form-item label="文章原链接" prop="old_url">-->
        <!--          <el-input-->
        <!--            v-model="form.url"-->
        <!--            type="textarea"-->
        <!--            :autosize="{ minRows: 2, maxRows: 4}"-->
        <!--            placeholder="请输入文章长链接地址"-->
        <!--          />-->
        <!--          <el-button type="primary">33</el-button>-->
        <!--        </el-form-item>-->

        <el-form-item label="文章长链接" prop="long_url">
          <el-input
            v-model="form.long_url"
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 4}"
            placeholder="请输入文章长链接地址"
          />
        </el-form-item>

        <el-form-item label="是否启用" prop="status">
          <el-switch
            v-model="form.status"
            :active-value="'启用'"
            :inactive-value="'禁用'"
          />
        </el-form-item>

        <el-row style="top: 30px;margin-bottom: 30px;text-align:center;">
          <el-col :span="24">
            <el-button type="primary" size="medium" @click="submitForm('form')">{{ title }}</el-button>
            <el-button size="medium" @click="close()">取消</el-button>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import { createOfficial, editOfficial } from '@/api/basics'
export default {
  name: 'AddEdit',
  props: {
    open: {
      type: Boolean
    },
    form: {
      type: Object
    },
    title: {
      type: String
    }
  },
  data() {
    return {
      rules: {
        official_name: [
          { required: true, message: '请填写公众号名称', trigger: 'blur' }
        ],
        old_url: [
          { required: true, message: '请填写长链接地址', trigger: 'blur' }
        ],
        long_url: [
          { required: true, message: '请填写长链接地址', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '请设置是否启用', trigger: 'blur' }
        ]
      },
      role: [],
      data: []
    }
  },
  created() {

  },
  methods: {
    submitForm(formName) {
      const that = this
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (that.title === '添加') {
            this.add()
          } else {
            this.edit()
          }
        } else {
          return false
        }
      })
    },
    add() {
      const that = this
      createOfficial(that.form).then(response => {
        if (response.code === 10000) {
          that.$message.success(response.message)
          that.$parent.getList()
          that.$emit('update:open', false)
        }
      })
    },
    edit() {
      const that = this
      editOfficial(that.form.id, that.form).then(response => {
        if (response.code === 10000) {
          that.$message.success(response.message)
          that.$parent.getList()
          that.$emit('update:open', false)
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    close() {
      this.$emit('update:open', false)
    }
  }
}
</script>

